package 枚举;

//所有自己写的枚举类都默认继承enum
public enum TestEnum {
    //用逗号隔开
    Red("red",1),
    Black("black",2),
    Green("green",1);
    private String color;
    private int ordinal;

    //默认private修饰


    TestEnum(String color,int ordinal) {
        this.color = color;
        this.ordinal = ordinal;
    }

    /**
     * 枚举和反射的集合
     * @param args
     */

    public static void main(String[] args) {
        //以数组的形式返回枚举类型的所有成员
        TestEnum[] testEnums = TestEnum.values();//这个方法在枚举类中没有
        for (TestEnum x:testEnums) {
            System.out.println(x+" 索引 "+x.ordinal()+"对应的ordinal : " + x.ordinal +"对应的color : "+x.color);
        }
        //获取这个枚举对象
        TestEnum t =TestEnum.valueOf("Red");
        System.out.println(t);
        //也可以进行比较
        System.out.println(Red.compareTo(Green));
    }
    public static void main1(String[] args) {
        TestEnum color = Red;
        //枚举可以作为switch的参数的
        switch (color) {
            case Red:
                System.out.println("Red");
                break;
            case Black:
                System.out.println("Black");
                break;
            case Green:
                System.out.println("Green");
                break;
            default:
                System.out.println("color error");
                break;
        }
    }
}
